import { DefaultCrudRepository, juggler, BelongsToAccessor, repository, RelationType, HasManyRepositoryFactory } from '@loopback/repository';
import { Post, User, Comment } from '../models';
import { MysqlLocalDataSource } from '../datasources';
import { inject, Getter } from '@loopback/core';
import { UserRepository } from './user.repository';
import { CommentRepository } from './comment.repository';

export class PostRepository extends DefaultCrudRepository<Post, typeof Post.prototype.id> {

  public readonly user: BelongsToAccessor<User, typeof Post.prototype.id>;

  // public readonly comments: HasManyRepositoryFactory<Comment, typeof Comment.prototype.id>;

  constructor(
    @inject('datasources.mysqlLocal') dataSource: MysqlLocalDataSource,
    @repository.getter('UserRepository')
    protected userRepositoryGetter: Getter<UserRepository>,
    // @repository.getter('CommentRepository')
    // protected commentRepositoryGetter: Getter<CommentRepository>,
  ) {
    super(Post, dataSource);

    this.user = this.createBelongsToAccessorFor('user', userRepositoryGetter);

    // this.comments = this.createHasManyRepositoryFactoryFor('comments', commentRepositoryGetter);

    // this.user = createBelongsToAccessor(
    //   {
    //     target: () => User,
    //     name: 'user',
    //     type: RelationType.belongsTo,
    //     source: Review,
    //     keyFrom: 'user_id',
    //     keyTo: 'id',
    //   },
    //   userRepositoryGetter,
    //   this,
    // );
  }
}
